import requests,re
from lxml import etree
from numpy.core.defchararray import title

nextlxml = '//div[@class="bottem1"]/a[3]/@href'
titlelxml = 'string(//div[@class="bookname"]/h1)'
infolxml = '//div[@id="content"]/text()'

def CatchText(url,pattern = '\\xa0|www.|Www.|wWw.|wwW.|52dus.com.coM|52dus.com.com|\\s{3,}'):
    global titlelxml,infolxml,nextlxml
    # 伪装
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0'
    }
    # 发请求
    response = requests.get(url, headers=headers)
    ##设置编码
    ##response.encoding = 'utf-8'
    # 处理
    e = etree.HTML(response.text)
    info = e.xpath(infolxml)
    title = e.xpath(titlelxml)
    title = re.sub(pattern, '', title)
    info = '\n'.join([re.sub(pattern, '', item)for item in info])
    next_url = e.xpath(nextlxml)[0]
    if next_url == "javascript:alert('已经是最后一章了');":
        next_url = False
    return title,info,next_url

#网址
url='https://www.wcxsw.la/book/650/1119.html'
pattern = '\\xa0|www.|Www.|wWw.|wwW.|52dus.com.coM|52dus.com.com|\\s{3,}'

back = CatchText(url,pattern)
# 打印结果
print(back[0])
# 输出文件
with open('小说.txt', 'a') as f:
    f.write(back[0] + '\n\n')
    f.write(back[1] + '\n\n\n')
for i in range(99):
    if back[2] == False:
        break
    nexturl = back[2]
    back = CatchText(f'https://www.wcxsw.la/{nexturl}', pattern)
    # 打印结果
    print(back[0])
    # 输出文件
    with open('小说.txt', 'a') as f:
        f.write(back[0] + '\n\n')
        f.write(back[1] + '\n\n\n')